package com.ruicar.afs.cloud.basic.creditchange.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.basic.api.dto.UpdateBankCardInfoDto;
import com.ruicar.afs.cloud.basic.api.dto.UpdateCustInfoDto;
import com.ruicar.afs.cloud.basic.common.entity.BasicBankCard;
import com.ruicar.afs.cloud.basic.common.entity.BasicContractCustRelation;
import com.ruicar.afs.cloud.basic.common.entity.BasicCustBaseInfo;
import com.ruicar.afs.cloud.basic.common.service.BasicBankCardService;
import com.ruicar.afs.cloud.basic.common.service.BasicContractCustRelationService;
import com.ruicar.afs.cloud.basic.common.service.BasicCustBaseInfoService;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author chuke
 * @create 2020/8/1 15:22
 */
@Slf4j
@RestController
@RequestMapping("/updateContract")
@AllArgsConstructor
public class UpdateContractInfoController {
    private final BasicCustBaseInfoService basicCustBaseInfoService;
    private final BasicBankCardService bankCardService;
    private final BasicContractCustRelationService relationService;
    private final BasicCustBaseInfoService custBaseInfoService;


    @ApiOperation(value = "更新身份信息")
    @PostMapping("/updateCustInfo")
    public IResponse updateCustInfo(@RequestBody UpdateCustInfoDto custInfo) {
        //身份信息表更新
        BasicCustBaseInfo custBaseInfo = basicCustBaseInfoService.getOne(Wrappers.<BasicCustBaseInfo>lambdaQuery()
                .eq(BasicCustBaseInfo::getCustNo, custInfo.getCustNo()));
        custBaseInfo.setCustName((StringUtils.isNotEmpty(custInfo.getNewName())) ? custInfo.getNewName() : null);
        custBaseInfo.setCertType((StringUtils.isNotEmpty(custInfo.getNewCertType())) ? custInfo.getNewCertType() : null);
        custBaseInfo.setCertNo((StringUtils.isNotEmpty(custInfo.getNewCertNo())) ? custInfo.getNewCertNo() : null);
        custBaseInfo.setCertStartDate((EmptyUtils.isNotEmpty(custInfo.getNewCertStartDate())) ? custInfo.getNewCertStartDate() : null);
        custBaseInfo.setCertEndDate((EmptyUtils.isNotEmpty(custInfo.getNewCertEndDate())) ? custInfo.getNewCertEndDate() : null);
        basicCustBaseInfoService.updateById(custBaseInfo);
        return new IResponse().success("身份信息修改成功");
    }


    @ApiOperation(value = "更新银行卡信息")
    @PostMapping("/updateBankInfo")
    public IResponse updateBankInfo(@RequestBody UpdateBankCardInfoDto dto) {
        BasicBankCard bankCard = bankCardService.getOne(Wrappers.<BasicBankCard>lambdaQuery()
                .eq(BasicBankCard::getContractNo, dto.getContractNo()));
        BasicCustBaseInfo baseInfo = custBaseInfoService.getOne(Wrappers.<BasicCustBaseInfo>lambdaQuery()
                .eq(BasicCustBaseInfo::getCertNo,dto.getCertNo()));
        if (dto.getNewCardholder() != null) {//变更银行表和个人信息表
            bankCard.setAccountName(baseInfo.getCustName());
            baseInfo.setCustName(baseInfo.getCustName());
        }
        bankCard.setCertNo(dto.getCertNo());
        bankCard.setAccountName(baseInfo.getCustName());
        bankCard.setBankCode(dto.getNewBank());
        bankCard.setBankBranch(dto.getNewBranchBank());
        bankCard.setAccountNo(dto.getNewBankAccount());
        bankCard.setOpenCity(dto.getProvince());
        bankCard.setOpenProvince(dto.getCity());
        bankCardService.updateById(bankCard);
        return new IResponse().success("银行卡信息修改完成");
    }
}
